package leetcode.D100.T81;

class Solution {
//    public boolean search(int[] nums, int target) {
//        int left = 0, right = nums.length-1;
//        while(left <= right) {
//            int mid = left + (right-left) / 2;
//            if(nums[mid] == target || nums[right] == target || nums[left] == target) {
//                return true;
//            }
//            if(nums[mid] < target && nums[right] > target) {
//                left = mid + 1;
//            } else if(nums[mid] > target && nums[left] < target) {
//                right = mid - 1;
//            } else {
//                left++;
//                right--;
//            }
//        }
//        return false;
//    }

    // 二刷
    public boolean search(int[] nums, int target) {
        int left = 0, right = nums.length-1;
        while(left <= right) {
            int mid = left + (right - left) / 2;
            if(nums[left] == target || nums[right] == target || nums[mid] == target) {
                return true;
            }
            if(nums[mid] < target && nums[right] > target) {
                left = mid + 1;
            } else if(nums[mid] > target && nums[left] < target) {
                right = mid - 1;
            } else {
                left++;
                right--;
            }
        }
        return false;
    }
}